package com.example.serviceconsumer.fallback;

import com.example.serviceconsumer.client.ProductClientA;
import com.example.serviceconsumer.model.Product;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class ProductClientAFallback implements ProductClientA {

    @Override
    public ResponseEntity<List<Product>> getAllProducts() {
        System.err.println("断路器A触发: getAllProducts服务降级响应 - 失败率阈值30%");
        // 返回一个默认的或空的列表，并给出提示信息
        List<Product> defaultList = new ArrayList<>();
        defaultList.add(new Product(-1L, "断路器A降级: 服务不可用", 0.0));
        return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).body(defaultList);
    }

    @Override
    public ResponseEntity<Product> createProduct(Product product) {
        System.err.println("断路器A触发: createProduct服务降级响应 - 失败率阈值30%");
        return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE)
                .body(new Product(-1L, "断路器A降级: 创建失败", product != null ? product.getPrice() : 0.0));
    }
}